package cc.momas.v1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sod-Momas
 * @since 2020/11/8
 */
public class LexerApplicationDemo {
    public static void main(String[] args) {
        String script = "my age 12 ";
        List<Token> tokenList = new ArrayList<>();
        StringBuilder curTokenText = new StringBuilder();
        TokenType curTokenType = TokenType.INITIAL;

        for (char ch : script.toCharArray()) {
            if (Character.isAlphabetic(ch)) {
                switch (ch) {
                    case 'i': curTokenType = TokenType.INT_1;break;
                    case 'n': curTokenType = TokenType.INT_2;break;
                    case 't': curTokenType = TokenType.INT_3;break;
                    default: curTokenType = TokenType.IDENTIFIER;break;
                }
                curTokenText.append(ch);
            } else if (Character.isDigit(ch)) {
                curTokenType = TokenType.NUMBER_LITERAL;
                curTokenText.append(ch);
            } else {
                if (0 < curTokenText.length()) {
                    // 已经是一个token的终结状态
                    tokenList.add(new Token(curTokenType, curTokenText.toString()));
                }
                curTokenText = new StringBuilder();
                curTokenType = TokenType.INITIAL;
            }
        }
        // 把最后一个token塞进去
        if (0 < curTokenText.
                length()) {
            // 已经是一个token的终结状态
            tokenList.add(new Token(curTokenType, curTokenText.toString()));
        }
        System.out.println(tokenList);
    }
}
